package com.yy.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yy.domain.AssetBuilding;
import com.yy.domain.AssetHouseBlock;
import com.yy.service.IAssetBuildingService;
import com.yy.service.IAssetHouseBlockService;
import com.yy.service.IAssetHouseService;
import com.yy.vo.TreeOptions;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yy.common.annotation.Log;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.enums.BusinessType;


import com.yy.common.utils.poi.ExcelUtil;
import com.yy.common.core.page.TableDataInfo;

/**
 * 楼栋Controller
 * 
 * @author yy
 * @date 2023-10-26
 */
@RestController
@RequestMapping("/asset/building")
public class AssetBuildingController extends BaseController
{
    @Autowired
    private IAssetBuildingService assetBuildingService;
    @Autowired
    private IAssetHouseBlockService assetHouseBlockService;
    /**
     * 查询楼栋列表
     */
    @PreAuthorize("@ss.hasPermi('system:building:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssetBuilding assetBuilding)
    {
        startPage();
        List<AssetBuilding> list = assetBuildingService.selectAssetBuildingList(assetBuilding);
        return getDataTable(list);
    }

    /**
     * 导出楼栋列表
     */
    @PreAuthorize("@ss.hasPermi('system:building:export')")
    @Log(title = "楼栋", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetBuilding assetBuilding)
    {
        List<AssetBuilding> list = assetBuildingService.selectAssetBuildingList(assetBuilding);
        ExcelUtil<AssetBuilding> util = new ExcelUtil<AssetBuilding>(AssetBuilding.class);
        util.exportExcel(response, list, "楼栋数据");
    }

    /**
     * 获取楼栋详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:building:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(assetBuildingService.selectAssetBuildingById(id));
    }

    /**
     * 新增楼栋
     */
    @PreAuthorize("@ss.hasPermi('system:building:add')")
    @Log(title = "楼栋", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssetBuilding assetBuilding)
    {
        return toAjax(assetBuildingService.insertAssetBuilding(assetBuilding));
    }

    /**
     * 修改楼栋
     */
    @PreAuthorize("@ss.hasPermi('system:building:edit')")
    @Log(title = "楼栋", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetBuilding assetBuilding)
    {
        return toAjax(assetBuildingService.updateAssetBuilding(assetBuilding));
    }

    /**
     * 删除楼栋
     */
    @PreAuthorize("@ss.hasPermi('system:building:remove')")
    @Log(title = "楼栋", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(assetBuildingService.deleteAssetBuildingByIds(ids));
    }
    /**
     * 查询商圈和楼栋的级联数据
     * [
     * {value:"1",label:"光谷金融港",children:[{}]},{}
     * ]
     */
    @GetMapping("getAllBuildingCascade")
    public AjaxResult getAllBuildingCascade(){
        List<TreeOptions> treeOptions=new ArrayList<>();
        //查询所有商圈
        List<AssetHouseBlock> assetHouseBlocks = assetHouseBlockService.selectAssetHouseBlockList(null);
        //查询所有楼栋
        List<AssetBuilding> assetBuildings = assetBuildingService.selectAssetBuildingList(null);
        for (AssetHouseBlock assetHouseBlock : assetHouseBlocks) {
            TreeOptions blockOption=new TreeOptions(assetHouseBlock.getId().toString(),assetHouseBlock.getName());
            //如果assetHouseBlock里面的id==assetBuildings里面的blockId就说明assetBuildings对象是assetHouseBlock子节点
            for (AssetBuilding assetBuilding : assetBuildings) {
                if(assetBuilding.getBlockId().equals(assetHouseBlock.getId())){
                    blockOption.getChildren().add(new TreeOptions(assetBuilding.getId().toString(),assetBuilding.getName()));
                }
            }
            //把blockOption放到treeOptions
            treeOptions.add(blockOption);
        }
        return AjaxResult.success(treeOptions);
    }


}
